package cn.bluethink.timescaledb.util;

import java.util.List;

public class PageUtil<T> {

        private int totalPage;

        private int pageNo;

        private List<T> items;

        private int pageSize;

        private int start;

        private int end;

        public PageUtil(int total,int pageNo,int pageSize) {

            this.pageSize = pageSize;

            this.totalPage = total / pageSize;

            if (total % pageSize != 0) {
                this.totalPage++;
            }

            if(totalPage < 1) {
                totalPage = 1;
            }

            if(pageNo > totalPage) {
                pageNo = totalPage;
            }

            if(pageNo < 1) {
                pageNo = 1;
            }

            this.pageNo = pageNo;

            this.start = (pageNo - 1) * pageSize;

            if (totalPage == 1){
                this.end = total;
            }else {
                this.end = start + this.pageSize;
            }

        }

        public int getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }

        public int getPageNo() {
            return pageNo;
        }

        public void setPageNo(int pageNo) {
            this.pageNo = pageNo;
        }

        public List<T> getItems() {
            return items;
        }

        public void setItems(List<T> items) {
            this.items = items;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getStart() {
            return start;
        }

        public void setStart(int start) {
            this.start = start;
        }

        public int getEnd() {
            return end;
        }

        public void setEnd(int end) {
            this.end = end;
        }
}
